package dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import tables.RouteTable;
import tables.TransportTable;

import com.epam.grsulab.webapi.dao.AbstractDao;

import entity.Route;
import entity.Transport;

public class TransportDao extends AbstractDao<TransportTable, Transport> {

	public TransportDao(final String rootFolderPath) {
		super(rootFolderPath);
	}

	@Override
	public void saveNew(final Transport newTransport) {
		newTransport.setId(getNextId());
		final TransportTable table = deserializeFromXml();
		table.getRows().add(newTransport);
		serializeToXml(table);
	}

	@Override
	public void update(final Transport entity) {
		final TransportTable table = deserializeFromXml();
		for (final Transport row : table.getRows()) {
			if (row.getId().equals(entity.getId())) {
				row.setRoute(entity.getRoute());
				row.setTime(entity.getTime());
				row.setType(entity.getType());
				break;
			}
		}
		serializeToXml(table);
	}
	
	public void updateRoute(final Transport entity) {
		final TransportTable table = deserializeFromXml();
		for (final Transport row : table.getRows()) {
			if (row.getId().equals(entity.getId())) {
				row.setRoute(entity.getRoute());
				row.setTime(entity.getTime());
				row.setType(entity.getType());
				break;
			}
		}
		serializeToXml(table);
	}

	@Override
	public Transport get(final Serializable id) {
		final TransportTable table = deserializeFromXml();
		for (final Transport row : table.getRows()) {
			if (row.getId().equals(id)) {
				return row;
			}
		}
		return null;
	}

	@Override
	public List<Transport> getAll() {
		final TransportTable flightTable = deserializeFromXml();
		return flightTable.getRows();
	}

	@Override
	public void delete(final Serializable id) {
		final TransportTable table = deserializeFromXml();
		Transport toBeDeleted = null;
		for (final Transport row : table.getRows()) {
			if (row.getId().equals(id)) {
				toBeDeleted = row;
				break;
			}
		}
		// remove from list
		table.getRows().remove(toBeDeleted);
		// save updated table
		serializeToXml(table);
	}

	public void deleteByRoute(String route) {
		final TransportTable table = deserializeFromXml();
		Transport toBeDeleted = null;
		for (final Transport row : table.getRows()) {
			if (row.getRoute().equals(route)) {
				toBeDeleted = row;
				break;
			}
		}
		table.getRows().remove(toBeDeleted);
		serializeToXml(table);
	}
	@Override
	protected Class<TransportTable> getTableClass() {
		return TransportTable.class;
	}
	public List<Transport> getAllAutobus() {
		final TransportTable catalogTable = deserializeFromXml();
		List<Transport> list= new ArrayList();
		for (Transport c : catalogTable.getRows()) {
			if(c.getType().equals("Autobus")) list.add(c);
		}
		return list;
	}
	
	public List<Transport> getAllTrolleybus() {
		final TransportTable catalogTable = deserializeFromXml();
		List<Transport> list= new ArrayList();
		for (Transport c : catalogTable.getRows()) {
			if(c.getType().equals("Trolleybus")) list.add(c);
		}
		return list;
	}
	public List<Transport> getAllTrum() {
		final TransportTable catalogTable = deserializeFromXml();
		List<Transport> list= new ArrayList();
		for (Transport c : catalogTable.getRows()) {
			if(c.getType().equals("Trum")) list.add(c);
		}
		return list;
	}
}
